package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.nio.ByteBuffer;

public class SubChoiceDataValid extends AxdrType {
    private ByteBuffer dataCode = null;
    private DataValidChoice choice = DataValidChoice._ERR_NONE_SELECTED;
    private SID sid = null;
    private SIDMAC sidMac = null;
    private RN rn = null;
    private RN_MAC rnMac = null;

    public SubChoiceDataValid() {
    }

    public void set_dataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setRn(RN rn) {
        resetChoices();
        this.choice = DataValidChoice.RN;
        this.rn = rn;
    }

    public void setRnMac(RN_MAC rnMac) {
        resetChoices();
        this.choice = DataValidChoice.RN_MAC;
        this.rnMac = rnMac;
    }

    public void setSID(SID sid) {
        resetChoices();
        this.choice = DataValidChoice.SID;
        this.sid = sid;
    }

    public void setSidMac(SIDMAC sidMac) {
        resetChoices();
        this.choice = DataValidChoice.SID_MAC;
        this.sidMac = sidMac;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = DataValidChoice.getEnumByValue((int)choosen.getValue());
        if (this.choice == DataValidChoice.SID_MAC) {
            this.sidMac = new SIDMAC();
            codeLength += this.sidMac.decode(input);
            return codeLength;
        } else if (this.choice == DataValidChoice.RN) {
            this.rn = new RN();
            codeLength += this.rn.decode(input);
            return codeLength;
        } else if (this.choice == DataValidChoice.RN_MAC) {
            this.rnMac = new RN_MAC();
            codeLength += this.rnMac.decode(input);
            return codeLength;
        } else if (this.choice == DataValidChoice.SID) {
            this.sid = new SID();
            codeLength += this.sid.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.capacity() - 1; i >= 0; i--) {
                output.write(this.dataCode.get(i));
            }
            return this.dataCode.capacity();
        }
        if (this.choice == DataValidChoice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == DataValidChoice.SID) {
            codeLength += this.sid.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(3);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == DataValidChoice.RN_MAC) {
            codeLength += this.rnMac.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(2);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == DataValidChoice.RN) {
            codeLength += this.rn.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == DataValidChoice.SID_MAC) {
            codeLength += this.sidMac.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    public DataValidChoice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = DataValidChoice._ERR_NONE_SELECTED;
        this.sid = null;
        this.sidMac = null;
        this.rn = null;
        this.rnMac = null;
    }

    public String toString() {
        if (this.choice == DataValidChoice.SID_MAC) {
            if (this.sidMac != null) {
                return "choice: {sidMac: " + this.sidMac + "}";
            } else {
                return "choice is sidMac but sidMac is null";
            }
        } else if (this.choice == DataValidChoice.SID) {
            if (this.sid != null) {
                return "choice: {sid: " + this.sid + "}";
            } else {
                return "choice is sid but sid is null";
            }
        } else if (this.choice == DataValidChoice.RN_MAC) {
            if (this.rnMac != null) {
                return "choice: {rnMac: " + this.rnMac + "}";
            } else {
                return "choice is rnMac but rnMac is null";
            }
        } else if (this.choice == DataValidChoice.RN) {
            if (this.rn != null) {
                return "choice: {rn: " + this.rn + "}";
            } else {
                return "choice is rn but rn is null";
            }
        } else {
            return "unknown";
        }
    }

    enum DataValidChoice {
        _ERR_NONE_SELECTED(-1),
        SID_MAC(0),
        RN(1),
        RN_MAC(2),
        SID(3);

        private final int value;

        DataValidChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static DataValidChoice getEnumByValue(int value) {
            for (DataValidChoice item : DataValidChoice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
